package org.kaer.election.ui;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.text.TextUtils;
import android.util.Log;

public class JsonUtil {
 
	/** ��ַ */
    //private static final String INNER_URL = "http://192.168.0.101:8080/meter/index.php/login/lgApp";
	//private static final String INNER_URL = "http://192.168.0.101:8080/test.php";	
    /** TAG */
	//private static String server = "http://192.168.0.104";
	private static String server = "http://218.58.186.112:9999";
    private final String TAG = getClass().getSimpleName();
    private static final int USER_ID = 13; //��ѯ�û���Ϣ
    private static final int SSSJ_ID = 14; //��ѯʵʱ������Ϣ
    private static final int SSBJ_ID = 15; //��ѯʵʱ������Ϣ
    private static final int SFBJ_ID = 16; //��ѯʵʱ������Ϣ
    
    /***
     * �ͻ��˵��õķ��������ݲ�����������з�������
     * 
     * @param userId
     * @param userName
     * @return
     */
    public static String getData(String userName, String password) {
        int modelId = USER_ID;
        
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        list.add(new BasicNameValuePair("username", userName));
        list.add(new BasicNameValuePair("password", password));
        try{
        	String sResult = (String)doPost(modelId, list, server + "/meter/index.php/login/lgApp");
        	return sResult;         	
        }catch(Exception e){
        	return "error";
        }        
    }
    
    public static String getDataSssj(String userName, String password) {
        int modelId = SSSJ_ID;
        
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        list.add(new BasicNameValuePair("username", userName));
 
        try{
        	return (String)doPost(modelId, list, server + "/meter/index.php/xtsy/search_app");        	
        }catch(Exception e){
        	return null;
        }
    }
    
    public static String getDataSsbj(String userName, String password) {
        int modelId = SSBJ_ID;
        
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        list.add(new BasicNameValuePair("username", userName));
 
        try{
        	return (String)doPost(modelId, list, server + "/meter/index.php/xtsy/search_appbj");        	
        }catch(Exception e){
        	return null;
        }
    }
    
    public static String getDataSfbj(String userName, String password) {
        int modelId = SFBJ_ID;
        
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        list.add(new BasicNameValuePair("username", userName));
 
        try{
        	return (String)doPost(modelId, list, server + "/meter/index.php/xtsy/search_appbjs");        	
        }catch(Exception e){
        	return null;
        }
    }
 
    /**
     * JsonStr�ַ���BOMͷ����
     * @param data
     * @return
     */
    public static final String removeBOM(String data) {

      if (TextUtils.isEmpty(data)) {
        return data;
      }

      if (data.startsWith("ufeff")) {
        //Log.e(TAG, "Json�ַ���BOMͷ����");
        return data.substring(1);
      } else {
        return data;
      }
    }
    
    /**
     * ����������ķ���
     * 
     * @param model
     * @param paramList
     * @return
     * @throws ParseException 
     */
    private static Object doPost(int model, List<NameValuePair> paramList, String INNER_URL1) throws ParseException {
 
        // 1.�����������
        HttpPost httpPost = new HttpPost(INNER_URL1);
        // post����ʽ���ݷ���ʵ������
        HttpEntity entity = null;
        try {
            entity = new UrlEncodedFormEntity(paramList, HTTP.UTF_8);
            httpPost.setEntity(entity);
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
 
        // 2.�����ͻ��˶���
        HttpClient httpClient = new DefaultHttpClient();
        // 3.�ͻ��˴���������������������
        try {
            // �������˷������������
            HttpResponse httpResponse = httpClient.execute(httpPost);
            // �������󷵻ص�����
            if (httpResponse != null
                    && httpResponse.getStatusLine().getStatusCode() == 200) {
                String element = EntityUtils.toString(httpResponse.getEntity(),
                        HTTP.UTF_8);
                //return "success";
                //if (element.startsWith("{")) {
                    try {
                    	if(model == USER_ID){                    		
                    		JSONObject json1 = new JSONObject(element);
                            String result1 = json1.getString("result");
                            //System.out.print("--------");
                            //System.out.print(result1);
                            return result1;	
                    	}else{
                    		//Gson gson = new Gson();
                    		
                    		//int len=element.length();
                    		//int i_start = element.indexOf("{");
                    		//String s3 = element.substring(i_start);
                    		return removeBOM(element);
                    	}/*if(model == SSSJ_ID ){
                    		return element;
                    	}else if(model == SSBJ_ID ){
                    		return element;
                    	}else if( model == SFBJ_ID){
                    		return element;
                    	}*/
                        
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            //}
 
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            return "error";
        }
 
        return "null";
 
    }
 
}